package com.tencent.cloud.iov.kernel.util;

import android.text.TextUtils;

import com.tencent.cloud.iov.kernel.constant.ApiConst;
import com.tencent.cloud.iov.kernel.constant.CookieConst;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;

/**
 * @author vinsonhuang
 * @date 2018/9/28
 */
public class CookieUtils {

    /**
     * preferences中userId key
     */
    public static String getUserIdKey(String host) {
        if (ApiConst.getHost().equals(host)) {
            return getApiUserIdKey();
        }
        return "";
    }

    /**
     * preferences中userSig key
     */
    public static String getUserSigKey(String host) {
        if (ApiConst.getHost().equals(host)) {
            return getApiUserSigKey();
        }
        return "";
    }

    /**
     * preferences中account sessionID key
     */
    public static String getAccountSessionIdKey(String host) {
        if (ApiConst.getHost().equals(host)) {
            return ApiConst.getHost() + "@" + CookieConst.IOV_ACCOUNT_SESSION_ID;
        }
        return "";
    }

    /**
     * preferences中access sessionID key
     */
    public static String getAccessSessionIdKey(String host) {
        if (ApiConst.getHost().equals(host)) {
            return ApiConst.getHost() + "@" + CookieConst.IOV_ACCESS_SESSION_ID;
        }
        return "";
    }

    public static String getGateWaySessionIdKey(String host) {
        if (ApiConst.getHost().equals(host)) {
            return ApiConst.getHost() + "@" + CookieConst.IAM_GATEWAY_SESSIONID;
        }
        return "";
    }

    public static String getUserIdValue() {
        String userId = TXSharedPreferencesUtils.getValue(getApiUserIdKey());
        if (TextUtils.isEmpty(userId)) {
            return "";
        }
        String[] userIdValues = userId.split(";");
        if (null == userIdValues || userIdValues.length < 1) {
            return "";
        }
        for (String userIdValue : userIdValues) {
            if (!TextUtils.isEmpty(userIdValue) && userIdValue.contains(CookieConst.USER_ID)) {
                String[] temp = userIdValue.split("=");
                if (null == temp || temp.length < 1) {
                    return "";
                }
                return temp[1];
            }
        }
        return "";
    }

    public static void clear() {
        TXSharedPreferencesUtils.remove(getApiUserIdKey());
        TXSharedPreferencesUtils.remove(getApiUserSigKey());
        TXSharedPreferencesUtils.remove(getApiAccountSessionIdKey());
        TXSharedPreferencesUtils.remove(getApiAccessSessionIdKey());
    }

    private static String getApiUserIdKey() {
        return ApiConst.getHost() + "@" + CookieConst.USER_ID;
    }

    private static String getApiUserSigKey() {
        return ApiConst.getHost() + "@" + CookieConst.USER_SIG;
    }

    private static String getApiAccountSessionIdKey() {
        return ApiConst.getHost() + "@" + CookieConst.IOV_ACCOUNT_SESSION_ID;
    }

    private static String getApiAccessSessionIdKey() {
        return ApiConst.getHost() + "@" + CookieConst.IOV_ACCESS_SESSION_ID;
    }
}
